﻿using Microsoft.Office.Interop.Visio;
using System;

namespace VisioERLeveller.Model {

    public enum LinkType {
        NotNull,
        Nullable
    }

    public class Link : GraphItem {

        public Link(Graph graph, string id, string name, string text, bool nullable, string fromId, string toId) : base(graph, id, name, text) {
            this.Type = nullable ? LinkType.Nullable : LinkType.NotNull;
            this.FromId = fromId;
            this.ToId = toId;
        }

        public string FromId { get; private set; }

        public string ToId { get; private set; }

        public LinkType Type { get; private set; }

        public Link CopyTo(Graph graph) {
            Link link = new(graph, this.ID, this.Name, this.Text, this.Type == LinkType.Nullable, FromId, ToId);
            link.Tag(this._Tag);
            return link;
        }

        public void Remove() {
            var from = this.Graph.GetNode(this.FromId);
            var to = this.Graph.GetNode(this.ToId);
            from.RemoveLink(this);
            to.RemoveLink(this);
        }

        public override string ToString() {
            var nullable = this.Type == LinkType.NotNull ? "-" : "0";
            var name = string.IsNullOrWhiteSpace(this.Name) ? "{NONAME}" : $"{this.Name}";
            var text = string.IsNullOrWhiteSpace(this.Text) ? string.Empty : $"({this.Text})";
            return $"LINK[{this.ID}]: {name}{text}: {this.FromId} -{nullable}-> {this.ToId}";
        }
    }
}
